dirPath=$1
fileName=$2
serialType=$3
filePath="${dirPath}/${fileName}.apk"
newApkFilePath="${dirPath}/签名-${fileName}.apk"


echo "接收到车型$serialType"
if [[ $serialType == "wutong" ]]
then
     echo 梧桐系
     serial=0xddb66eefd98476f3 
elif [[ $serialType == "feiyu" ]]
then
     echo 飞鱼系
     serial=0xd42599c0446bdafc
else
     echo 没有对应该车型
fi

cd $dirPath

echo ------------------------------生成私钥：private.key---------------------;
openssl genrsa -out private.key 2048

echo ------------------------------生成v1证书：csr.csr、private.key---------------------;
echo 证书的serial为 $serial
openssl req -new -key private.key -out csr.csr -subj "/emailAddress=auto_release@auto-pai.com/CN=SCM/OU=Software/O=WTCL/L=HaiDian/ST=Beijing/C=CN"
echo serial的值是$serial
openssl x509 -req -in csr.csr -signkey private.key -out certificate.crt -days 18250 -set_serial $serial

echo ------------------------------将证书和私钥导出到PKCS12格式的密钥库文件：certificate.crt、cert.p12---------------------;
openssl pkcs12 -export -in certificate.crt -inkey private.key -out cert.p12 -name cert -passout pass:123456789

echo ------------------------------生成jks：cert.jks---------------------;
keytool -importkeystore -srckeystore cert.p12 -srcstorepass 123456789 -srcstoretype PKCS12 -destkeystore cert.jks -deststoretype JKS -deststorepass 123456789 -noprompt

echo ------------------------------签名---------------------;
apksigner sign --ks cert.jks --ks-key-alias "cert" --ks-pass pass:123456789 --out $newApkFilePath $filePath

echo ------------------------------签名结束---------------------;
